import request from '@/utils/request'

// 注册接口
//export const userRegisterService = ({ username, password, repassword, id }) =>
//  request.get('http://127.0.0.1:8000/register/?username=' + username + '&password=' + password + '&id=' + id,{
//    headers:{
//        'Origin': 'http://127.0.0.1:5173'
//    }
//  })
export const userRegisterService = async ({ username, password, repassword, id }) => {
  try {
    const response = await fetch('http://127.0.0.1:8000/register/?username=' + username + '&password=' + password + '&id=' + id, {
      method: 'GET'
    });

    if (response.ok) {
      const data = await response.json();
      // 处理 JSON 响应数据
      console.log('Login successful:', data);
      return data;
    } else {
      console.error('Login failed with status:', response.status);
      return null;
    }
  } catch (error) {
    console.error('Error during login:', error);
    return null;
  }
}


// 登录接口
//export const userLoginService = ({ username, password }) =>
//  request.get('http://127.0.0.1:8000/login/?username='+username+'&password='+password)
export const userLoginService = async ({ username, password }) => {
  try {
    const response = await fetch('http://127.0.0.1:8000/login/?username='+username+'&password='+password, {
      method: 'GET'
    });

    if (response.ok) {
      const data = await response.json();
      // 处理 JSON 响应数据
      console.log('Login successful:', data);
      return data;
    } else {
      console.error('Login failed with status:', response.status);
      return null;
    }
  } catch (error) {
    console.error('Error during login:', error);
    return null;
  }
};

// 获取用户基本信息
export const userGetInfoService = async (username) => {
  try {
    const response = await fetch('http://127.0.0.1:8000/getUserInfo/?username='+username, {
      method: 'GET'
    });

    if (response.ok) {
      const data = await response.json();
      // 处理 JSON 响应数据
      console.log('getinfo successful:', data);
      return data;
    } else {
      console.error('Login failed with status:', response.status);
      return null;
    }
  } catch (error) {
    console.error('Error during login:', error);
    return null;
  }
};

// 更新用户基本信息
export const userUpdateInfoService = ({ id, nickname, email }) =>
  request.put('/my/userinfo', { id, nickname, email })

// 更新用户头像
export const userUpdateAvatarService = (avatar) =>
  request.patch('/my/update/avatar', { avatar })

// 更新用户密码
export const userUpdatePasswordService = ({ old_pwd, new_pwd, re_pwd }) =>
  request.patch('/my/updatepwd', { old_pwd, new_pwd, re_pwd })
